package com.linkhub.oms.common.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.linkhub.oms.common.enums.ResponseCodeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @Description: DataResponse
 * @Author: echo
 * @Date: 2024/3/2 21:29
 * @Version: 1.0
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class DataResponse<T> {

    public static final String CODE_SUCCESS = "0";

    public static final String DEFAULT_ERROR_CODE = "1";

    @ApiModelProperty("0： 成功， 其他： 失败")
    private String code;
    @ApiModelProperty("错误消息")
    private String msg;
    @ApiModelProperty("接口数据")
    private T data;

    public static <T> DataResponse<T> success() {
        DataResponse<T> response = new DataResponse<>();
        response.setResponseCode(ResponseCodeEnum.SUCCESS);
        return response;
    }

    public static <T> DataResponse<T> success(T data) {
        DataResponse<T> response = new DataResponse<T>();
        response.setResponseCode(ResponseCodeEnum.SUCCESS);
        response.setData(data);
        return response;
    }


    public static <T> DataResponse<T> fail(String code, String msg) {
        DataResponse<T> response = new DataResponse<>();
        response.setCode(code);
        response.setMsg(msg);
        return response;
    }

    public static <T> DataResponse<T> fail(String code, String msg, T data) {
        DataResponse<T> response = new DataResponse<>();
        response.setCode(code);
        response.setMsg(msg);
        response.setData(data);
        return response;
    }

    public static <T> DataResponse<T> fail(ResponseCodeEnum code) {
        DataResponse<T> res = new DataResponse<>();
        res.setResponseCode(code);
        return res;
    }

    public static <T> DataResponse<T> fail(ResponseCodeEnum code, String msg) {
        DataResponse<T> res = new DataResponse<>();
        res.setCode(code.getCode());
        res.setMsg(msg);
        return res;
    }

    protected void setResponseCode(ResponseCodeEnum code) {
        this.code = code.getCode();
        this.msg = code.getMsg();
    }

}
